package com.gzyj.web.api.sys;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzyj.data.common.PageData;
import com.gzyj.data.common.ResponseMsg;
import com.gzyj.pojo.anbiao.AnbiaoVehicle;
import com.gzyj.web.config.SysConfig;
import com.gzyj.web.mapper.veh.AnbiaoVehicleMapper;
import com.gzyj.web.model.VehicleInfo;
import com.gzyj.web.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("api/sys/clgl")
public class ClglApi {
    @Autowired
    private AnbiaoVehicleMapper anbiaoVehicleMapper;


    @Autowired
    RedisTemplate<String, Object> clusterRedisTemplate;

    @Value("${redis.key.vehicle.info}")
    private String RedisKey_VehInfo;

    @PostMapping("list")
    public ResponseMsg<PageData<VehicleInfo>> getList(VehicleInfo param) {

        List<VehicleInfo> list = anbiaoVehicleMapper.getList(param);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<VehicleInfo> rs= list.stream().map(e -> {
            if (e.getGpstime() != null) {
                int secs = DateUtils.secStrToNowTime(e.getGpstime());
                boolean online = (0 < secs && secs < SysConfig.onlineTime * SysConfig.minuteSec) ? true : false;
                if (online) {
                    if (e.getSpeed() > 0) {
                        Integer secsGps = DateUtils.secToNowTime(e.getGpstime());
                        if (secsGps > 0 && secsGps < 30 * 60) {
                            e.setRunStatus("行驶");
                        } else {
                            if (e.getAcc() == 1) {
                                e.setRunStatus("停车");
                            } else {
                                e.setRunStatus("熄火");
                            }
                        }
                    } else {
                        if (e.getAcc() == 1) {
                            e.setRunStatus("停车");
                        } else {
                            e.setRunStatus("熄火");
                        }
                    }
                } else {
                    if (SysConfig.neverOnlineTime.equalsIgnoreCase(sdf.format(e.getGpstime()))) {
                        e.setRunStatus("从未上线");
                    }else{
                        e.setRunStatus("离线");
                    }
                }
            } else {
                e.setRunStatus("从未上线");
            }


            return e;
        }).collect(Collectors.toList());

        return new ResponseMsg<>(200, "success", new PageData<>(anbiaoVehicleMapper.getCount(param), rs));
    }

    @PostMapping("update")
    public ResponseMsg<Object> update(AnbiaoVehicle param){
        param.setCaozuoshijian(new Date());
        anbiaoVehicleMapper.updateById(param);
        clusterRedisTemplate.opsForValue().set(RedisKey_VehInfo+param.getZongduanid()+":"+param.getId()+":"+param.getCheliangpaizhao()+param.getChepaiyanse()+":"+param.getPlatformconnectiontype(),param);

        return new ResponseMsg<>(200,"success");
    }

    @PostMapping("add")
    public ResponseMsg<Object> add(AnbiaoVehicle param){
        param.setCaozuoshijian(new Date());
        param.setCreatetime(new Date());
        Integer vehicle= anbiaoVehicleMapper.selectCount(new QueryWrapper<AnbiaoVehicle>().eq("zongduanid",param.getZongduanid()).or().eq("cheliangpaizhao",param.getCheliangpaizhao()));
        if(vehicle>0){
            return new ResponseMsg<>(201,"SIM卡已绑定车辆");
        }
        anbiaoVehicleMapper.insert(param);
        clusterRedisTemplate.opsForValue().set(RedisKey_VehInfo+param.getZongduanid()+":"+param.getId()+":"+param.getCheliangpaizhao()+param.getChepaiyanse()+":"+param.getPlatformconnectiontype(),param);
        return new ResponseMsg<>(200,"success");
    }

    @PostMapping("del")
    public ResponseMsg<Object> del(VehicleInfo param){
        anbiaoVehicleMapper.deleteById(param.getId());
        return new ResponseMsg<>(200,"success");
    }

}
